package com.th.admin.controller;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.th.admin.bean.User;
import com.th.admin.exception.UserTooManyException;
import com.th.admin.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.servlet.mvc.support.RedirectAttributes;

import java.lang.reflect.Array;
import java.util.Arrays;
import java.util.List;

/**
 * @author ThreePure
 * @date 2022/4/18 20:45
 * @description: TODO
 * @since 1.8
 */
@Controller
public class TableController {

    @Autowired
    UserService userService;

    @GetMapping("/basic_table")
    public String basic_table(){
        //int a = 10/0;
        return "table/basic_table";
    }

    @GetMapping("/dynamic_table")
    public String dynamic_table(@RequestParam(value = "pn",defaultValue = "1")Integer pn,  Model model){
        /*//表格内容的遍历
        List<User> users = Arrays.asList(new User("张三", "123456"),
                new User("李四", "234567"),
                new User("王五", "345678"),
                new User("赵六", "456789"),
                new User("钱七", "123456"));
        model.addAttribute("users",users);*/

        /*if(users.size()>3){
            throw new UserTooManyException();//抛出自定义异常
        }*/

        //从数据库中查出所有user表中数据
        List<User> list = userService.list();
        Page<User> userPage = new Page<>(pn, 2);
        Page<User> page = userService.page(userPage, null);


        model.addAttribute("page", page);

        return "table/dynamic_table";
    }

    @GetMapping("/user/delete/{id}")
    public String deleteUser(@PathVariable("id") Long id,
                             @RequestParam(value = "pn",defaultValue = "1")Integer pn,
                             RedirectAttributes ra){

        userService.removeById(id);

        ra.addAttribute("pn",pn);
        return "redirect:/dynamic_table";
    }

    @GetMapping("/responsive_table")
    public String responsive_table(){
        return "table/responsive_table";
    }

    @GetMapping("/editable_table")
    public String editable_table(){
        return "table/editable_table";
    }


}
